package com.noname.lib_base_java.db.download;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.noname.lib_base_java.global.GlobalApplication;
import com.noname.lib_base_java.net.download.DownloadInfo;

/**
 * Author: Lmoumou
 * Date: 2019-01-22 15:16
 * 文件名: DowanDBManager
 * 描述:
 */
public class DowanloadDBManager {
    private static final String TAG = "DemoDBManager";
    private static DowanloadDBManager dbMgr = new DowanloadDBManager();
    private DbDownloadOpenHelper dbHelper;

    private DowanloadDBManager() {
        dbHelper = DbDownloadOpenHelper.getInstance(GlobalApplication.getContext());
    }

    public static synchronized DowanloadDBManager getInstance() {
        if (dbMgr == null) {
            dbMgr = new DowanloadDBManager();
        }
        return dbMgr;
    }


    /**
     * 保存数据
     *
     * @param info
     */
    public synchronized void saveContact(DownloadInfo info) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DownloadDao.FILE_NAME, info.getFileName());
        if (info.getSavePath() != null) {
            values.put(DownloadDao.SAVE_PATH, info.getSavePath());
        }
        values.put(DownloadDao.CONTENT_LENGTH, info.getContentLength());
        values.put(DownloadDao.READ_LENGTH, info.getReadLength());
        if (info.getUrl() != null) {
            values.put(DownloadDao.URL, info.getUrl());
        }
        values.put(DownloadDao.ZIP_SUC, info.isZipSuc());
        if (db.isOpen()) {
            db.replace(DownloadDao.TABLE_NAME, null, values);
        }
    }

    /**
     * 获取单个user
     */
    public synchronized DownloadInfo getContact(String fileName) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.query(DownloadDao.TABLE_NAME, new String[]{
                            DownloadDao.SAVE_PATH,
                            DownloadDao.CONTENT_LENGTH,
                            DownloadDao.READ_LENGTH,
                            DownloadDao.URL,
                            DownloadDao.FILE_NAME,
                            DownloadDao.ZIP_SUC}, DownloadDao.FILE_NAME + "=?",
                    new String[]{fileName}, null, null, null);
            if (cursor.moveToNext()) {
                String savePath = cursor.getString(cursor.getColumnIndex(DownloadDao.SAVE_PATH));
                long contentLength = cursor.getLong(cursor.getColumnIndex(DownloadDao.CONTENT_LENGTH));
                long readLength = cursor.getLong(cursor.getColumnIndex(DownloadDao.READ_LENGTH));
                String url = cursor.getString(cursor.getColumnIndex(DownloadDao.URL));
                String fileName1 = cursor.getString(cursor.getColumnIndex(DownloadDao.FILE_NAME));
                int zipSuc = cursor.getInt(cursor.getColumnIndex(DownloadDao.ZIP_SUC));

                DownloadInfo info = new DownloadInfo();
                info.setSavePath(savePath);
                info.setContentLength(contentLength);
                info.setReadLength(readLength);
                info.setUrl(url);
                info.setFileName(fileName1);
                info.setZipSuc(zipSuc);
                cursor.close();
                return info;
            }
        }
        return null;
    }

    /**
     * 更新单个user的数据
     */
    public synchronized void updataInfo(DownloadInfo info) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db.isOpen()) {
            ContentValues values = new ContentValues();
            values.put(DownloadDao.SAVE_PATH, info.getSavePath());
            values.put(DownloadDao.CONTENT_LENGTH, info.getContentLength());
            values.put(DownloadDao.READ_LENGTH, info.getReadLength());
            values.put(DownloadDao.URL, info.getUrl());
            values.put(DownloadDao.FILE_NAME, info.getFileName());
            values.put(DownloadDao.ZIP_SUC, info.isZipSuc());
            db.update(DownloadDao.TABLE_NAME, values, DownloadDao.FILE_NAME + "=?", new String[]{info.getFileName()});
        }
    }
}
